import random
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.globals import ChartType
import streamlit as st
import streamlit_echarts
from streamlit_echarts import st_pyecharts
import streamlit.components.v1 as components
import pandas as pd


def get_province_full_name(province_short_names):
    china_provinces_full_names = {
        "北京": "北京市",
        "天津": "天津市",
        "上海": "上海市",
        "重庆": "重庆市",
        "河北": "河北省",
        "山西": "山西省",
        "辽宁": "辽宁省",
        "吉林": "吉林省",
        "黑龙江": "黑龙江省",
        "江苏": "江苏省",
        "浙江": "浙江省",
        "安徽": "安徽省",
        "福建": "福建省",
        "江西": "江西省",
        "山东": "山东省",
        "河南": "河南省",
        "湖北": "湖北省",
        "湖南": "湖南省",
        "广东": "广东省",
        "海南": "海南省",
        "四川": "四川省",
        "贵州": "贵州省",
        "云南": "云南省",
        "陕西": "陕西省",
        "甘肃": "甘肃省",
        "青海": "青海省",
        "台湾": "台湾省",
        "内蒙古": "内蒙古自治区",
        "广西": "广西壮族自治区",
        "西藏": "西藏自治区",
        "宁夏": "宁夏回族自治区",
        "新疆": "新疆维吾尔自治区",
        "香港": "香港特别行政区",
        "澳门": "澳门特别行政区"
    }
    return [china_provinces_full_names.get(province) for province in province_short_names]


provinces = [
    "北京", "天津", "上海", "重庆", "河北", "山西", "辽宁", "吉林", "黑龙江",
    "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东",
    "海南", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾", "内蒙古", "广西",
    "西藏", "宁夏", "新疆", "香港", "澳门"
]
df = pd.read_csv('./结果文件/董宇辉/董宇辉1.csv')
df['rounded_sentiment_score'] = df['sentiment_score'].round()
# 2. 根据IP_location进行分组，并计算sentiment_score的平均值
# 3. 如果IP_location为空，则将其默认为"未知"
df['IP_location'].fillna('未知', inplace=True)
grouped_df = df.groupby('IP_location')['rounded_sentiment_score'].mean().reset_index()
emotion_values = [0] * len(provinces)
for i, province in enumerate(provinces):
    if province in grouped_df['IP_location'].values:
        emotion_values[i] = grouped_df[grouped_df['IP_location'] == province]['rounded_sentiment_score'].values[0]
provinces = get_province_full_name(provinces)
sentiment_values = {province: value for province, value in zip(provinces, emotion_values)}
print(sentiment_values)
from pyecharts.datasets import register_url

# register_url('https://echarts-maps.github.io/echarts-china-counties-js/')
map_data = [(key, value) for key, value in sentiment_values.items()]
map = (
    Map()
    .add('情感均值', map_data, "china")
    .set_global_opts(
        title_opts=opts.TitleOpts(title="中国情感均值地图"),
        visualmap_opts=opts.VisualMapOpts(is_piecewise=True,
                                          pieces=[{"max": 0, "min": -20, "label": "-20-0", "color": "#00FFFF"},
                                                  {"max": 20, "min":0 , "label": "0-10", "color": "#FF69B4"},
                                                  {"max": 20, "min": 40, "label": "20-40", "color": "#0000FF"},
                                                  {"max": 40, "min": 60, "label": "40-60", "color": "#00BFFF"},
                                                  {"max": 60, "min": 200, "label": "60-200", "color": "#228B22"},
                                                  {"max": 3000, "min": 2000, "label": "2000-3000", "color": "#FF0000"},
                                                  {"max": 20000, "min": 10000, "label": ">=10000", "color": "#FFD700"}
                                                  ])
    )
)
# 渲染地图
map.render("china_emotion_map.html")
china_emotion_map = map.render_embed()  # 保存为HTML文件
# st.write(china_emotion_map,unsafe_allow_html=True)
# st_pyecharts(map,width="100%",height="600px")
components.html(china_emotion_map, height=600)
